<?php
namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Custom extends BaseModel
{
    protected $connection = 'api';
    protected $userTable = 'user';
    protected $room_table = 'room';//房间表
    protected $union_room = 'union_room';//公会房间
    protected $union = 'union';//公会表
    protected $giftTable = 'gift_library';//礼物表
    protected $goodsTable = 'mall_goods';//商品表

    protected $activityConn = 'activity';
    protected $custom_table = 'custom';
    protected $custom_rank_config_table = 'custom_rank_config';
    protected $custom_rank_reward_config_table = 'custom_rank_reward_config';
    protected $custom_rank_event_table = 'custom_rank_event';
    protected $custom_rank_reward_log_table = 'custom_rank_reward_log';
    protected $custom_lyb_config_table = 'custom_lyb_config';
    protected $custom_ticket_config_table = 'custom_ticket_config';
    protected $custom_ticket_game_type_table = 'custom_ticket_game_type';
    protected $custom_ticket_game_table = 'custom_ticket_game';
    protected $customRuleTable = 'custom_rule';
    protected $custom_recharge_config_table = 'custom_recharge_config';
    protected $custom_bridge_config_table = 'custom_bridge_config';
    protected $custom_bridge_award_config_table = 'custom_bridge_award_config';


    //自定义活动新增活动规则
    public function customRuleAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRuleAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动编辑活动规则
    public function customRuleEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRuleEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动删除活动规则
    public function customRuleDel($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRuleDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动规则列表
    public function customRule($page, $limit, $custom_id){
        $where = function($q)use($custom_id){
            $q->where('is_deleted',0);
            if(!empty($custom_id)){
                $q->where('custom_id',$custom_id);
            }
        };
        $res = DB::connection($this->activityConn)->table($this->customRuleTable)->where($where)->paginate($limit,['*'],'page',$page);
        return $res;
    }

    //自定义活动编辑人气票配置
    public function customTicketConfigEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customTicketConfigEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //获取人气票配置
    public function customTicketConfig(){
        $res = DB::connection($this->activityConn)->table($this->custom_ticket_config_table)->where('id',1)->first();
        $res = objToArray($res);
        return $res;
    }

    //获取游戏类型
    public function customTicketGameType(){
        $res = DB::connection($this->activityConn)->table($this->custom_ticket_game_type_table)->get();
        $res = objToArray($res);
        return $res;
    }

    //自定义活动新增人气票游戏
    public function customTicketGameAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customTicketGameAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动新增人气票游戏
    public function customTicketGameEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customTicketGameEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动删除人气票游戏
    public function customTicketGameDel($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customTicketGameDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动人气票游戏列表
    public function customTicketGame($page, $limit, $custom_id){
        $where = function($q)use($custom_id){
            $q->where('is_deleted',0);
            if(!empty($custom_id)){
                $q->where('custom_id',$custom_id);
            }
        };
        $res = DB::connection($this->activityConn)->table($this->custom_ticket_game_table)->where($where)->paginate($limit,['*'],'page',$page);
        $res = objToArray($res);
        if(!empty($res['data'])){
            foreach($res['data'] as $k=>$v){
                $res['data'][$k]['img_url'] = build_image($v['img_url']);
            }
        }
        return $res;
    }

    //自定义活动清空数据
    public function customClear($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customClear', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动新增
    public function customAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动编辑
    public function customEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动榜单新增
    public function customRankAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRankAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动榜单编辑
    public function customRankEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRankEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动榜单删除
    public function customRankDel($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRankDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动榜单奖励新增
    public function customRankRewardAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRankRewardAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动榜单奖励编辑
    public function customRankRewardEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRankRewardEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动榜单奖励删除
    public function customRankRewardDel($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRankRewardDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动列表
    public function customList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $custom_id = $request->input('custom_id', 0);
        $game_name = $request->input('game_name', '');
        $date_st = $request->input('date_st', '');
        $date_ed = $request->input('date_ed', '');

        $query = DB::connection($this->activityConn)->table($this->custom_table)->orderBy('id', 'desc')->where('custom_type', 1);
        if(!empty($custom_id)) {
            $query = $query->where('id', $custom_id);
        }
        if(!empty($game_name)) {
            $query = $query->where('game_name', 'like', '%'.$game_name.'%');
        }
        if(!empty($date_st)) {
            $time_st = date("Y-m-d H:i:s", strtotime($date_st));
            $query = $query->where('created_at', '>=', $time_st);
        }
        if(!empty($date_ed)) {
            $time_ed = date("Y-m-d H:i:s", strtotime($date_ed));
            $query = $query->where('created_at', '<=', $time_ed);
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        foreach($result['data'] as $key=>$val) {

            $result['data'][$key] = $val;
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //自定义活动榜单列表
    public function customRankList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $custom_id = $request->input('custom_id', 0);

        $query = DB::connection($this->activityConn)->table($this->custom_table.' as a')
            ->join($this->custom_rank_config_table.' as b', "a.id", "=", "b.custom_id")
            ->orderBy('a.id', 'desc')
            ->select(['b.id','b.custom_id','a.game_name','b.rank_type','b.if_day','b.if_all','b.score_rule'])
            ->where('b.is_delete', 0)->where('a.custom_type', 1);
        if(!empty($custom_id)) {
            $query = $query->where('a.id', $custom_id);
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        foreach($result['data'] as $key=>$val) {

            $result['data'][$key] = $val;
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //自定义活动榜单奖励列表
    public function customRankRewardList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $custom_id = $request->input('custom_id', 0);
        $rank_id = $request->input('rank_id', 0);

        $query = DB::connection($this->activityConn)->table($this->custom_table.' as a')
            ->join($this->custom_rank_config_table.' as b', "a.id", "=", "b.custom_id")
            ->join($this->custom_rank_reward_config_table.' as c', "b.id", "=", "c.rank_id")
            ->orderBy('a.id', 'desc')
            ->orderBy('b.id', 'desc')
            ->select(['c.id','b.custom_id','a.game_name','c.rank_id','b.rank_type','c.rank_num','c.reward_need_min','c.reward_type','c.data_id','c.data_num','c.receive_type','c.day_type'])
            ->where('b.is_delete', 0)->where('a.custom_type', 1);
        if(!empty($custom_id)) {
            $query = $query->where('a.id', $custom_id);
        }
        if(!empty($rank_id)) {
            $query = $query->where('b.id', $rank_id);
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        foreach($result['data'] as $key=>$val) {
            $dataName = '';
            //金豆
            if(in_array($val['reward_type'], [6])) {
                $dataName = '金豆';
            }
            //钻石
            if(in_array($val['reward_type'], [7])) {
                $dataName = '钻石';
            }
            //商品
            if(in_array($val['reward_type'], [1,2,4,5])) {
                $good = DB::connection($this->connection)->table($this->goodsTable)
                    ->where('goods_id', $val['data_id'])
                    ->first();
                $dataName = empty($good->goods_name) ? 0 : $good->goods_name;
            }
            //礼物
            if(in_array($val['reward_type'], [3])) {
                $gift = DB::connection($this->connection)->table($this->giftTable)
                    ->where('gift_id', $val['data_id'])
                    ->first();
                $dataName = empty($gift->name) ? 0 : $gift->name;
            }
            $val['data_name'] = $dataName;

            $result['data'][$key] = $val;
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //自定义活动留言板新增
    public function customLybAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customLybAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动留言板编辑
    public function customLybEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customLybEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动留言板列表
    public function customLybList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $custom_id = $request->input('custom_id', 0);

        $query = DB::connection($this->activityConn)->table($this->custom_table.' as a')
            ->join($this->custom_lyb_config_table.' as b', "a.id", "=", "b.custom_id")
            ->orderBy('a.id', 'desc')
            ->select(['b.id','b.custom_id','a.game_name','b.diamand_num','b.lyb_url','b.content','b.full_id'])
            ->where('b.is_delete', 0)->where('a.custom_type', 1);
        if(!empty($custom_id)) {
            $query = $query->where('a.id', $custom_id);
        }

        $result = $query->paginate($limit, ['*'], 'page', $page);
        $result = objToArray($result);
        foreach($result['data'] as $key=>$val) {
            $val['lyb_url'] = build_image($val['lyb_url']);

            $result['data'][$key] = $val;
        }
        $result = ['data'=>$result['data'], 'total'=>$result['total']];

        return $result;
    }

    //自定义活动新增充值任务
    public function customRechargeAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRechargeAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动编辑充值任务
    public function customRechargeEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRechargeEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义任务删除充值任务
    public function customRechargeDel($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customRechargeDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义充值任务列表
    public function customRechargeList($page, $limit, $custom_id){
        $data = DB::connection($this->activityConn)->table($this->custom_recharge_config_table)->where('is_deleted',0)->where('custom_id',$custom_id)->orderBy('sort','DESC')->paginate($limit,['*'],'page',$page);
        return $data;
    }

    //自定义活动盖楼新增任务
    public function customBridgeAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customBridgeAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动盖楼编辑任务
    public function customBridgeEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customBridgeEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动盖楼删除任务
    public function customBridgeDel($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customBridgeDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动盖楼配列表
    public function customBridgeList($page, $limit, $custom_id){
        $data = DB::connection($this->activityConn)->table($this->custom_bridge_config_table)
        ->where('is_delete',0)
        ->where('custom_id',$custom_id)
        ->paginate($limit,['*'],'page',$page);
        return $data;
    }

    //自定义活动盖楼奖励新增
    public function customBridgeAwardAdd($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customBridgeAwardAdd', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动盖楼奖励编辑
    public function customBridgeAwardEdit($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customBridgeAwardEdit', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动盖楼奖励删除
    public function customBridgeAwardDel($data){
        $res = curl_request(getKkaudioActivityUrl() . '/inner/custom/customBridgeAwardDel', $data, 'urlencoded', 'POST');
        return curlResultHandle($res);
    }

    //自定义活动盖楼奖励列表
    public function customBridgeAwardList($page, $limit, $bridgeId){
        $data = DB::connection($this->activityConn)
        ->table($this->custom_bridge_award_config_table)->where('is_delete',0)
        ->where('bridge_id',$bridgeId)
        ->orderBy('level','ASC')
        ->paginate($limit,['*'],'page',$page);
        return $data;
    }
}
